package br.com.pfm.dao.impl;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.pfm.dao.InstituicaoDao;
import br.com.pfm.persistence.model.Instituicao;

@Repository
public class InstituicaoDaoImpl implements InstituicaoDao {

	@PersistenceContext(unitName="pfm")
    private EntityManager em;
	
	@Override
	public ArrayList<Instituicao> getList(int idCliente) {
		
		 Query query = em
				.createQuery("SELECT i FROM Instituicao as i where i.cliente.id = :pIdCliente");
				 		 
		 query.setParameter("pIdCliente", idCliente);
		 
		 @SuppressWarnings("unchecked")
		 ArrayList<Instituicao> instituicoes =  (ArrayList<Instituicao>) query.getResultList();
		 
		 return instituicoes;
	}
	
	@Override
	public Instituicao getById(int idInstituicao){		
		return em.find(Instituicao.class, idInstituicao); 		
	}
	
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	@Override
	public void adiciona(Instituicao instituicao) {
		em.persist(instituicao);
	}

}
